package cn.tedu.tmall.admin.mall.controller;

import cn.tedu.tmall.admin.mall.pojo.param.CategoryAddNewParam;
import cn.tedu.tmall.admin.mall.pojo.vo.CategoryListItemVO;
import cn.tedu.tmall.admin.mall.service.ICategoryService;
import cn.tedu.tmall.common.pojo.vo.PageData;
import cn.tedu.tmall.common.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Repository;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Api(tags = "1.类别管理")
@RestController
@RequestMapping("/categories")
public class CategoryController {
    @Autowired
    private ICategoryService service;

    //http://localhost:8080/categories/add-new?name=cat&parentId=0
    @ApiOperation("新增类别")
    @PreAuthorize("hasAuthority('/mall/category/simple')")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public JsonResult AddNew(@Validated CategoryAddNewParam categoryAddNewParam){
        service.addNew(categoryAddNewParam);
        return JsonResult.ok();
    }

    @ApiOperation("根据ID删除类别")
    @PreAuthorize("hasAuthority('/mall/category/delete')")
    @ApiOperationSupport(order = 200)
    @PostMapping("/{id}/delete")
    @ApiImplicitParam(name = "id", value = "类别ID", dataType= "long", required = true)
    public JsonResult deleteById(@PathVariable Long id){
        service.deleteById(id);
        return JsonResult.ok();
    }

    @ApiOperation("启用类别")
    @ApiOperationSupport(order = 301)
    @PostMapping("/{id}/enable")
    @ApiImplicitParam(name = "id", value = "类别ID", dataType= "long", required = true)
    public JsonResult setEnable(@PathVariable Long id){
        service.setEnable(id);
        return JsonResult.ok();
    }

    @ApiOperation("禁用类别")
    @ApiOperationSupport(order = 302)
    @PostMapping("/{id}/disable")
    @ApiImplicitParam(name = "id", value = "类别ID", dataType= "long", required = true)
    public JsonResult setDisable(@PathVariable Long id){
        service.setDisable(id);
        return JsonResult.ok();
    }

    @ApiOperation("显示类别")
    @ApiOperationSupport(order = 303)
    @PostMapping("/{id}/display")
    @ApiImplicitParam(name = "id", value = "类别ID", dataType= "long", required = true)
    public JsonResult setDisplay(@PathVariable Long id){
        service.setDisplay(id);
        return JsonResult.ok();
    }

    @ApiOperation("隐藏类别")
    @ApiOperationSupport(order = 304)
    @PostMapping("/{id}/hide")
    @ApiImplicitParam(name = "id", value = "类别ID", dataType= "long", required = true)
    public JsonResult setHide(@PathVariable Long id){
        service.setHide(id);
        return JsonResult.ok();
    }


    @ApiOperation("根据父级查询子级列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "parentId", value = "父级ID", dataType= "long", required = true),
            @ApiImplicitParam(name = "page", value = "页码", dataType = "int"),
    })
    @GetMapping("/list-by-parentId")
    public JsonResult listByParentId(Long parentId, Integer page){
        Integer pageNum = page;
        if(page == null || page<1){
            pageNum = 1;
        }
        PageData<CategoryListItemVO> pageData = service.listByParentId(parentId, pageNum);
        return JsonResult.ok(pageData);
    }



}
